#include<iostream>
#include<string>
#include<map>
using namespace std;
const int N = 1e6 + 10;
int arr[N];
int n;
bool fun1(int x)
{
	bool flag = 0;
	while (x) {
		if (x % 10 == 6) {
			flag = 1;
			break;
		}
		x /= 10;
	}
	if (flag) {
		return 1;
	}
	return 0;
}
void fun()
{
	int j = 1;
	for (int i = 1; i <= 1e6; i++) {
		if (fun1(i)) {
			arr[i] = arr[i - 1] + 1;
		}
		else {
			arr[i] = arr[i - 1];
		}
	}
}
int main()
{
	cin >> n;
	fun();
	while (n--) {
		int x;
		cin >> x;
		cout << x - arr[x] << endl;
	}
	return 0;
}